<?php

class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('costumer');
        $this->load->model('deposit');
        $this->load->model('ym');
        $this->load->model('user');
        $this->load->model('document');
        $this->load->model('booking');
        $this->load->model('costumer');
        $this->load->library('recaptcha');
        $this->load->helper('document_helper');
        $this->load->helper('user_helper');
    }

    function costumer() {
        $data['yms'] = $this->ym->admin();
        $data['documents'] = $this->document->index();
        $data['content'] = 'login/costumer';
        $this->load->view('template/login', $data);
    }

    function register() {
        $data['captcha'] = $this->recaptcha->recaptcha_get_html();
        $data['name'] = "";
        $data['username'] = "";
        $data['email'] = "";
        $data['last_name'] = "";
        $data['address'] = "";
        $data['home_phone'] = "";
        $data['mobile_phone'] = "";
        $data['country'] = "";
        $data['city'] = "";
        $data['state'] = "";
        $data['zip_code'] = "";
        $data['wrong'] = "";
        $data['username_e'] = '';
        $data['email_e'] = '';
        //Bank 
//        $data['bank_name'] = "";
//        $data['an'] = "";
//        $data['no_rekening'] = "";

        $data['yms'] = $this->ym->admin();
        $data['documents'] = $this->document->index();
        $data['date'] = date('Y-m-d');
        $data['content'] = 'login/register';
        $this->load->view('template/login', $data);
    }

    function create() {
        $this->load->library('form_validation');
        $name = $this->form_validation->set_rules('name', 'Nama', 'required');
        $username = $this->form_validation->set_rules('username', 'Usernmae', 'required|min_length[5]|max_length[12]|is_unique[users.username]');
        $email = $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
        $password = $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]|max_length[12]|required|matches[verify_password]');
        $verify_password = $this->form_validation->set_rules('verify_password', 'Ulangi password', 'required');
        $mobile_phone = $this->form_validation->set_rules('mobile_phone', 'Hp', 'required');

        $data['captcha'] = $this->recaptcha->recaptcha_get_html();
        $data['name'] = $this->input->post('name', TRUE);
        $data['username'] = $this->input->post('username', TRUE);
        $data['email'] = $this->input->post('email', TRUE);
        $data['last_name'] = $this->input->post('last_name', TRUE);
        $data['address'] = $this->input->post('address', TRUE);
        $data['home_phone'] = $this->input->post('home_phone', TRUE);
        $data['mobile_phone'] = $this->input->post('mobile_phone', TRUE);
        $data['country'] = $this->input->post('country', TRUE);
        $data['city'] = $this->input->post('city', TRUE);
        $data['state'] = $this->input->post('state', TRUE);
        $data['zip_code'] = $this->input->post('zip_code', TRUE);
        $data['date'] = date('Y-m-d');

        $chek = $this->costumer->cek_username(anti_xss($this->input->post('username', TRUE)));
        $chek_email = $this->costumer->cek_email(anti_xss($this->input->post('email', TRUE)));
        $resp = $this->recaptcha->recaptcha_check_answer($this->input->server('REMOTE_ADDR'), $this->input->post('recaptcha_challenge_field'), $this->input->post('recaptcha_response_field'), array(), true);


        if ($this->form_validation->run() == FALSE || count($resp) == 2 || count($chek) == 1 || count($chek_email) == 1) {
//        if ($this->form_validation->run() == FALSE) {

            if (count($resp) == 2) {
                $data['wrong'] = "Captcha Wrong";
                $data['username_e'] = '';
                $data['email_e'] = '';
            } elseif (count($chek) == 1) {
                $data['username_e'] = 'Username Already Used';
                $data['wrong'] = "";
                $data['email_e'] = '';
            } elseif (count($chek_email) == 1) {
                $data['email_e'] = 'Email Already Used';
                $data['wrong'] = '';
                $data['username_e'] = '';
            } else {
                $data['wrong'] = '';
                $data['username_e'] = '';
                $data['email_e'] = '';
            }
            $data['yms'] = $this->ym->admin();
            $data['documents'] = $this->document->index();
            $data['content'] = 'login/register';
            $this->load->view('template/login', $data);
//            redirect('login/register');
        } else {
            $name = anti_xss($this->input->post('name', TRUE));
            $username = anti_xss($this->input->post('username', TRUE));
            $email = anti_xss($this->input->post('email', TRUE));
            $password = anti_xss($this->input->post('password', TRUE));
            $password = md5($password);
            $last_name = anti_xss($this->input->post('last_name', TRUE));
            $address = anti_xss($this->input->post('address', TRUE));
            $home_phone = anti_xss($this->input->post('home_phone', TRUE));
            $mobile_phone = $this->input->post('mobile_phone', TRUE);
            $country = anti_xss($this->input->post('country', TRUE));
            $city = anti_xss($this->input->post('city', TRUE));
            $state = anti_xss($this->input->post('state', TRUE));
            $zip_code = anti_xss($this->input->post('zip_code', TRUE));
            $date = date('Y-m-d');

            $link = md5($username);
            $data['link'] = md5($username);
            $user_confirmation = 1;

//            $message = $this->load->view('login/r_email', $data);
//            $this->load->view('login/r_email', $data);
            $message = $this->load->view('login/r_email', $data, TRUE);
            $this->load->library('email');
            $this->email->set_mailtype("html");
            $this->email->from('stasiuntiketonline.com');

            //email
//            $list = array_merge($email);
            $this->email->to($email);
            $this->email->cc('');
            $this->email->subject('Registration');
            $this->email->message($message);
            $this->email->send();
            
            $this->costumer->create($name, $username, $email, $password, $last_name, $address, $home_phone, $mobile_phone, $country, $city, $state, $zip_code, $user_confirmation, $link, $date);
            echo '
            <script>
                var answer = alert("Thanks for register, Confirm your email");
                    if (!answer) {
                window.location="../login/costumer"
                 }
            </script>';
//            exit;
            //Email
        }
    }

    function logout() {
        $this->session->unset_userdata('logged_in');
        redirect('login/costumer', 'refresh');
    }

    function forgotpass() {
        $data['yms'] = $this->ym->admin();
        $data['documents'] = $this->document->index();
        $data['content'] = 'login/forgotpass';
        $this->load->view('template/login', $data);
    }

    function sendpassword() {
        $data['yms'] = $this->ym->admin();
        $data['documents'] = $this->document->index();

        $this->load->library('form_validation');
        $email = $this->form_validation->set_rules('email', 'Email', 'required');

        if ($this->form_validation->run() == FALSE) {
            $data['email'] = $this->input->post('email', TRUE);
            $data['content'] = 'login/forgotpass';
            $this->load->view('template/login', $data);
        } else {
            $date = date("y-m-d h:i:s");
            $email = $this->input->post('email', TRUE);

            $check = $this->costumer->check_e($email);
            if ($check == 0):
                redirect('login/forgotpass/gagal');
            else:
                $link = md5($email . $date);
                //Email
                $this->load->library('email');
                $this->email->from('chaerul.developer@gmail.com', 'chaerul');
                $this->email->to($email);
                $this->email->cc('');
                $this->email->subject('Registrer');
                $this->email->message("Click This link to reset password http://stasiuntiketonline.com/confirm/newpass/$link");
                $this->email->send();
                //end

                $this->costumer->sendpassword($email, $date, $link);
            endif;
        }
    }

    function resetpass() {
        $password = $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]|max_length[12]|required|matches[repeat_password]');
        $verify_password = $this->form_validation->set_rules('repeat_password', 'Repeat Password', 'required');

        if ($this->form_validation->run() == FALSE) {
            $link = $this->input->post('link', TRUE);
            redirect('confirm/newpass/' . $link);
        } else {
            $email = $this->input->post('email', TRUE);
            $password = $this->input->post('password', TRUE);
            $password = md5($password);
            $this->costumer->costumer_password($email, $password);
        }
    }

    function resetfinish() {
        echo "berhasil";
    }

    function crone_delete() {
        $date = date("Y-m-d");
        $gets = $this->booking->get_booking($date);
        foreach ($gets as $row):
            $this->delete_job($row->status, $row->expire, $row->code);
        endforeach;
    }

    function delete_job($status, $expire, $code) {

        $date = date('Y-m-d h:i:s');
        $date = date('h:i', strtotime($date));
        $date = explode(':', $date);
        $h_c = $date[0];
        $i_c = $date[1];

        $expire = date('h:i', strtotime($expire));
        $expire = explode(':', $expire);
        $h_e = $expire[0];
        $i_e = $expire[1];

        if ($h_c >= $h_e && $i_c >= $h_e && $status == 0):
            $delete = $this->booking->crone_delete($code);
        endif;
    }

    function limit_anggota() {
        $date = date("Y-m-d");
        $gets = $this->costumer->get_costumer($date);
        foreach ($gets as $row):
            $this->ambil($row->id);
        endforeach;
    }

    function ambil($costumer_id) {

        $price = 10000;
        $tgl = date('Y-m-d');
        $keterangan = 'Biaya Administrasi bulanan Rp 10000';
        $this->deposit->administrasi($costumer_id, $price, $tgl, $keterangan, $tgl);

        $date = mktime(0, 0, 0, date("m"), date("d") + 30, date("y"));
        $expire = date("Y/m/d", $date);
        $this->costumer->expire_tambah($costumer_id, $expire);
        return true;
    }

    function download() {
        $url = $this->uri->segment(3);
//        $url = 'cokelat_bendera_a';
//        $name = get_download($url);
        $file = get_file($url);
        $data = file_get_contents("upload/" . $file);
        $name = $file;
        force_download($name, $data);
    }

}

?>